<?php

namespace api\wechat;

class active_person
{

    //获取全量列表
    public function getList($data)
    {
        $name = $data['name'];
        $page = (int)$data['page'];
        $size = 20;
        $start = ($page - 1) * $size;

        $con = con();
        $where = " where a.is_delete=1 and a.status=1 and c.status=1 and c.is_delete=1 ";
        if (!empty($name)) {
            $where .= " and b.name like '%$name%'";
        }
        $limit = " order by a.`id` desc limit $start,$size";
        $sql = "select a.*,b.class,b.org,b.name,b.sex,b.nation,b.job,b.phone from active_person a left join guest b on (a.guest_id=b.id) left join active c on (a.active_id=c.id) $where";
        $data = mysqli_query($con, $sql . $limit);
        $data = mysqli_fetch_all($data, true);
        $total = mysqli_query($con, $sql);
        $total = mysqli_num_rows($total);
        $power=mysqli_query($con,"select * from setting where id=11");
        $power = mysqli_fetch_all($power,true);
        $power=json_decode($power[0]['value_1'],true);
        foreach ($power as $key1 => $value1) {
            if(!$value1){
                $power_false[$key1]=$value1;
            }
        }
        for ($i = 0; $i < count($data); $i++) {

            if( $data[$i]['start_time']<10000000){
                $data[$i]['start_time']='';
            }else{
                $data[$i]['start_time'] = date('Y-m-d H:i:s', $data[$i]['start_time']);
            }
            if( $data[$i]['end_time']<10000000){
                $data[$i]['end_time']='';
            }else{
                $data[$i]['end_time'] = date('Y-m-d H:i:s', $data[$i]['end_time']);
            }
            $data[$i]['trip_active'] = [];
            $data[$i]['trip_eat'] = [];

                foreach ($data[$i] as $key => $value) {
                    if($value===null){
                        $data[$i][$key]='';
                    }
                    if(!empty($power_false)){
                        foreach ($power_false as $key1 => $value1) {
                            if($key===$key1){
                                $data[$i][$key] = '';
                            }
                        }
                    }
                }


        }

//        $active_person_ids = array_column($data, 'id');
//        $active_person_ids = array_unique($active_person_ids);
//        $active_person_ids = implode(',', $active_person_ids);
//        if (!empty($active_person_ids)) {
//            $trip = mysqli_query($con, "select * from active_person_trip where active_person_id in ($active_person_ids) and is_delete=1 and status=1");
//            $trip = mysqli_fetch_all($trip, true);
//            for($i=0; $i<count($trip); $i++) {
//                $trip[$i]['start_time'] = date('Y-m-d H:i:s', $trip[$i]['start_time']);
//                $trip[$i]['end_time'] = date('Y-m-d H:i:s', $trip[$i]['end_time']);
//            }
//            for ($i = 0; $i < count($data); $i++) {
//                for ($o = 0; $o < count($trip); $o++) {
//                    if ($data[$i]['id'] == $trip[$o]['active_person_id']) {
//                        switch ($trip[$o]['type']) {
//                            case '1'://活动行程
//                                $data[$i]['trip_active'][] = $trip[$o];
//                                break;
//                            case '2'://就餐行程
//                                $data[$i]['trip_eat'][] = $trip[$o];
//                                break;
//                        }
//                    }
//                }
//            }
//        }
        mysqli_close($con);
        return out_right([
            'data' => $data,
            'page' => $page,
            'size' => $size,
            'total' => $total,
            'total_page' => ceil($total / $size)
        ]);
    }

}